JavaScript数组去重的两种方法

标签:2023-06-24 19:17:06

// indexof 去重
var arr =[1,6,12,0,4,7,6,3];
function unique(arr){
    var arr1 = [];       
    // 新建一个数组来存放arr中的值
    for(var i=0,len=arr.length;i<len;i  ){
       if(arr1.indexOf(arr[i]) === -1){
       arr1.push(arr[i]);
    }
  }
  return arr1;
}
console.log(unique(arr)); // [1, 6, 12, 0, 4, 7, 3]
 
// set  去重
let arr = [212, 3, 25, 3, 6, 7, 25];
let item = [...new Set(arr)];
console.log(item); // [212, 3, 25, 6, 7]
原文出处:http://www.dongblog.com/notes/9.html
来源:博客网 转载请注明出处!

活跃用户

Bingo~
Ta还没有签名
夏天丢了春姑娘
Ta还没有签名
59420
Ta还没有签名
明明蝶恋花
Ta还没有签名

友情链接


Warning: Smarty error: unable to read resource: "../../../templates/default/./common/foot/footer_index.htm" in /usr/home/hyu3925200001/htdocs/common/smarty/Smarty.class.php on line 1093

Warning: Smarty error: unable to read resource: "../../../templates/default/./common/foot/footer_index.htm" in /usr/home/hyu3925200001/htdocs/common/smarty/Smarty.class.php on line 1093